package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyValueAOUForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyValueFindForm;
import com.woniuxy.lrz.carlproject.model.GoodPropertyValue;
import com.woniuxy.lrz.carlproject.service.GoodPropertyValueService;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsPropertyValueDto;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsValuePropertyDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 属性值表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Api(tags = "属性值接口")
@Validated
@RequestMapping("/carlproject/goodPropertyValue")
public class GoodPropertyValueController {
    @Resource
    private GoodPropertyValueService goodPropertyValueService;

    @PostMapping("/addGoodPropertyValue")
    @ApiOperation("新增/修改属性值")
    public Results addGoodProperty(@RequestBody @Valid GoodsPropertyValueAOUForm goodsPropertyValueAOUForms){
        goodPropertyValueService.addGoodProperty(goodsPropertyValueAOUForms);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteGoodPropertyValue")
    @ApiOperation("删除属性值")
    @ApiImplicitParam(name = "propertyValueId",value = "属性值Id")
    public Results deleteGoodPropertyValue(@NotNull(message = "属性值Id不能为空") Integer propertyValueId){
        goodPropertyValueService.deleteGoodPropertyValue(propertyValueId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findGoodPropertyValue")
    @ApiOperation("加载商品属性值信息")
    public Results findGoodPropertyValue(@Valid GoodsPropertyValueFindForm goodsPropertyValueFindForm){
        Page<GoodsPropertyValueDto> goodPropertyValue = goodPropertyValueService.findGoodPropertyValue(goodsPropertyValueFindForm);
        return Results.instance().setMessage("加载属性值信息成功").setData(goodPropertyValue);
    }

    @GetMapping("/findPropertyValueByPropertyId")
    @ApiOperation("加载商品属性值下拉列表")
    @Cacheable(value = "goods",key = "'goodsPropertyValue:'+#propertyId")
    public Results findPropertyValueByPropertyId(@NotNull(message = "商品属性Id不能为空") Integer propertyId){
        List<GoodPropertyValue> property_id = goodPropertyValueService.list(new QueryWrapper<GoodPropertyValue>().eq("property_id", propertyId));
        List<GoodsValuePropertyDto> goodsValuePropertyDtos = BeanUtil.copyToList(property_id, GoodsValuePropertyDto.class);
        return Results.instance().setData(goodsValuePropertyDtos).setMessage("加载商品属性值下拉列表成功");
    }

}
